<template>
  <el-dialog
    :title="'创建家庭组'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    append-to-body>
    <el-form class="verify-form" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="家庭组名称" prop="familyGroupName">
      <el-input v-model="dataForm.familyGroupName" placeholder="家庭组名称"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :loading="ischeck">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
    import {addObj, putObj} from '@/api/eims/familygroup'
    import { getObj } from "@/api/admin/dept"
    import {mapGetters} from 'vuex'
    export default {
    data () {
      return {
        visible: false,
        ischeck: false,
        dataForm: {
          id: 0,
          familyGroupName: '',
        },
        dataRule: {
          familyGroupName: [
            { required: true, message: '家庭组名称不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    computed: {
      ...mapGetters(['userInfo'])
    },
    methods: {
      init (customerId) {
        this.dataForm.customerId = customerId || 0
        this.dataForm.familyGroupName = ''
        this.visible = true
        this.dataForm.deptId = this.userInfo.lessDeptId
        getObj(this.userInfo.lessDeptId).then(response => {
          if(response.data.code == '0'){
            this.dataForm.unitId = response.data.data.unitId
            this.dataForm.tenantId = response.data.data.tenantId
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.ischeck = true
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
                putObj(this.dataForm).then(response => {
                  if(response.data.code == '0'){
                    this.$message.success('修改成功')
                    this.visible = false
                    this.$emit('refreshData')
                  }else{
                    this.$message.error(response.data.msg)
                  }
                  this.ischeck = false 
                }).catch(() => {
                  this.ischeck = false
                })
            } else {
                addObj(this.dataForm).then(response => {
                  if(response.data.code == '0'){
                    this.$message.success('添加成功')
                    this.visible = false
                    this.$emit('refreshData')
                  }else{
                    this.$message.error(response.data.msg)
                  }
                  this.ischeck = false 
                }).catch(() => {
                  this.ischeck = false
                })
            }
          }else{
            this.ischeck = false
          }
        })
      }
    }
  }
</script>
